<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="24">
        <el-form :inline="true" @submit.native.prevent :model="queryParams">
          <el-form-item label="物料代码：">
            <el-input
              v-model="queryParams.bmBmyz"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              plain
              icon="el-icon-search"
              size="mini"
              @click="handleSearch"
              >搜索</el-button
            >

            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['project:newBm:add']"
              >新增</el-button
            >

            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['project:newBm:edit']"
            >
              修改
            </el-button>

            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['project:newBm:remove']"
            >
              删除
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      ref="multipleTable"
      @row-click="rowClick"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="mstrList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="匹配规则"
        :show-overflow-tooltip="true"
        width="200"
        align="center"
        prop="bmBmyz"
      />
      <el-table-column
        label="仓库"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bmStorageId"
      >
        <template #default="scope">
          <dict-tag
            :options="allStorageOptions"
            :value="scope.row.bmStorageId"
            keyName="id"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="优先级"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="priority"
      />
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        align="center"
        prop="bmDesc"
      />
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:newBm:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:newBm:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="编码原则" prop="bmBmyz">
            <el-input
              v-model="queryParams.bmBmyz"
              placeholder="请输入编码原则"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="仓位Id" prop="bmStorageId">
            <el-input
              v-model="queryParams.bmStorageId"
              placeholder="请输入仓位Id"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="描述信息" prop="bmDesc">
            <el-input
              v-model="queryParams.bmDesc"
              placeholder="请输入描述信息"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="优先级" prop="priority">
            <el-input
              v-model="queryParams.priority"
              placeholder="请输入优先级"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改仓位对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料编码" prop="bmBmyz">
          <el-input v-model="form.bmBmyz" placeholder="请输入编码匹配" />
        </el-form-item>
        <el-form-item label="仓位Id" prop="bmStorageId">
          <el-select v-model="form.bmStorageId" placeholder="请选择仓库代码">
            <el-option
              v-for="dict in storageOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述信息" prop="bmDesc">
          <el-input v-model="form.bmDesc" placeholder="请输入描述信息" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入优先级" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMstr,
  getMstr,
  delMstr,
  addMstr,
  updateMstr,
  exportMstr,
  getUpdateStorage,
} from "@/api/repertory/newStorageManage";
import mixins from "@/mixins/common";
export default {
  name: "NewStorageManage",
  mixins: [mixins.allStorageOptions, mixins.storageOptions],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 仓位表格数据
      mstrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        bmId: null,
        bmBmyz: null,
        bmStorageId: null,
        bmDesc: null,
        priority: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      wldm: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询仓位列表 */
    getList() {
      this.loading = true;
      this.queryParams.pageNum = 1;
      listMstr(this.queryParams).then((response) => {
        this.mstrList = [];
        this.mstrList = this.mstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listMstr(this.queryParams).then((response) => {
          this.mstrList = this.mstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bmBmyz: null,
        bmStorageId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        bmDesc: null,
        priority: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.mstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 搜索按钮操作 */
    handleQuery2() {
      if (!this.wldm) return;
      getUpdateStorage(this.wldm).then((res) => {
        this.mstrList = res.data;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bmBmyz);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //const bmBmyz = row.bmBmyz || this.ids
      getMstr(row.bmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓位";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询仓位";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bmId != null) {
            updateMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bmBmyzs = row.bmBmyz || this.ids;
      this.$confirm(
        '是否确认删除仓位编号为"' + bmBmyzs + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delMstr(row.bmId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有仓位数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    rowClick() {
      //点击行触发，选中或不选中复选框
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },
  },
};
</script>

<style lang="scss" scoped></style>
